/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.inftel.primeproject.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.management.Query;
import javax.persistence.EntityManager;
import javax.persistence.NamedQuery;
import javax.persistence.PersistenceContext;
import org.inftel.primeproject.model.Users;
import org.inftel.primeproject.remote.UsersFacadeRemote;


/**
 *
 * @author inftel
 */
@Stateless
public class UserFacade extends AbstractFacade<Users> implements UsersFacadeRemote {
    @PersistenceContext(unitName = "primeproject-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(Users.class);
    }
    
    @Override
    public Users findByMail(String email){
        List<Users> l = (List<Users>) getEntityManager().createNamedQuery("Users.findByEmail")
            .setParameter("email", email)
            .setMaxResults(1)
            .getResultList();         
        if (l.size() == 1){
            l.get(0).getUsersCollection().size();
            l.get(0).getUsersCollection1().size();        
            return l.get(0);
        }
        else{
            return null;
        }
    }
    
    
    @Override
    public long countBySex(char sex){
        long n = (Long) getEntityManager().createQuery("select count(u.id) FROM Users u WHERE u.sex = :sex")
            .setParameter("sex", sex)
            .getSingleResult();                 
        return n;
       
    }
}
